package com.example.memosystem.dao.impl;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.memosystem.bean.Record;
import com.example.memosystem.dao.impl.impl.AdminDaoImpl;
import com.example.memosystem.util.DBUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressLint("Recycle")
public class AdminDao implements AdminDaoImpl {
    SQLiteDatabase db = DBUtil.db;

    @Override
    public int registerAdmin(String ... da) {

        try{
            db.execSQL("INSERT INTO d_admin VALUES(?,?,?,?,?,?)", da);
            return 0;
        }catch (Exception e){
            return -1;
        }

    }

    @Override
    public int loginAdmin(String... da) {
        int a = 0;
        Cursor res = db.rawQuery("select * from d_admin where s_id=? and s_pwd=?", da);
        if(res.moveToNext())
        {
            a++;
        }
        return a;
    }

    @Override
    public int changePwdAdmin(String... da) {

        ContentValues values = new ContentValues();
        values.put("s_pwd", da[1]);
        String[] das = {da[0]};
        try{
            return db.update("d_admin", values, "s_id=?", das);
        }catch(Exception e)
        {
            return -1;
        }

    }

    @Override
    public int changeMesAdmin(String... da) {
        ContentValues values = new ContentValues();
        values.put("s_name", da[1]);
        values.put("s_sex", da[2]);
        values.put("s_phone", da[3]);
        values.put("s_age", da[4]);
        String[] das = {da[0]};
        try{
            return db.update("d_admin", values, "s_id=?", das);
        }catch(Exception e)
        {
            return -1;
        }
    }

    @SuppressLint("Recycle")
    @Override
    public Map<String, String> showAccountMesAdmin(String account) {
        Map<String,String> map = new HashMap<>();
        String[] da = {account};
        Cursor result = db.rawQuery("select s_name,s_sex,s_phone,s_age from d_admin where s_id=?",da);
        if(result.moveToNext())
        {
            String name = result.getString(0);
            String sex = result.getString(1);
            String phone = result.getString(2);
            String age = result.getString(3);

            map.put("name", name);
            map.put("sex", sex);
            map.put("phone", phone);
            map.put("age", age);


        }

        return map;
    }

    @Override
    public int delRecord(String... da) {
        try{
            db.execSQL("delete from d_record where s_id=? and s_uid = ?", da);
            return 0;
        }catch (Exception e){
            return -1;
        }
    }

    @Override
    public int saveRecord(String... da) {//id uid title con time
        try{
            db.execSQL("INSERT INTO d_record VALUES(?,?,?,?,?)", da);
            return 0;
        }catch (Exception e){
            String[] das = {da[2],da[3],da[4],da[0], da[1]};
            try{
                db.execSQL("update d_record set s_title=?,s_con=?,s_time=? where s_id=? and s_uid=?", das);
                return 0;
            }catch(Exception et)
            {
                e.printStackTrace();
                et.printStackTrace();
                return -1;
            }
        }
    }

    @Override
    public List<Record> showRecord(String u_id) {

        String[] das = {u_id};
        List<Record> re = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from d_record where s_uid=?", das);
        while(cursor.moveToNext())
        {
            Record record = new Record(cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4));
            re.add(record);

        }



        return re;
    }
}
